package com.common.tool;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.xml.XppDomDriver;

/**
 * xml json和对象的相互转换类
 * @author penghui.li
 *
 */
public class XmlBean {
	public XmlBean() {
	}

	public static String parseObj2Xml(Object obj) {
		String xmlStr = "";
		try {
			XStream sm = new XStream(new XppDomDriver());
			xmlStr = sm.toXML(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return xmlStr;
	}

	public static Object parseXml2Obj(String xml) {
		Object obj = null;
		try {
			XStream sm = new XStream(new XppDomDriver());
			obj = sm.fromXML(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}

	public static Object parseJson2Obj(String xml) {
		Object obj = null;
		try {
			XStream sm = new XStream(new JettisonMappedXmlDriver());
			obj = sm.fromXML(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}

	public static String parseObj2Json(Object obj) {
		String xmlStr = "";
		try {
			XStream sm = new XStream(new JettisonMappedXmlDriver());
			xmlStr = sm.toXML(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return xmlStr;
	}
}
